package hu.bme.aut.consulttime.client.components;

import java.util.List;

import hu.bme.aut.consulttime.client.ProjectService;
import hu.bme.aut.consulttime.client.ProjectServiceAsync;
import hu.bme.aut.consulttime.entitydtos.ProjectDTO;
import hu.bme.aut.consulttime.entitydtos.UserDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;

public class ProjectPage extends Composite {
	//Proxy
	private static ProjectServiceAsync projectService = GWT
			.create(ProjectService.class);
	
	private static VerticalPanel mainPanel;
	private static FlexTable flexTable = null;

	public ProjectPage() {
		
		mainPanel = new VerticalPanel();
		initWidget(mainPanel);
		
		Button btnCreate = new Button("Create New Project");
		btnCreate.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new CreateProject().center();
			}
		});
		mainPanel.add(btnCreate);
		mainPanel.setCellHorizontalAlignment(btnCreate, HasHorizontalAlignment.ALIGN_RIGHT);
		
		//loadProjects();
	}

	public static void loadProjects() {
		if (flexTable != null) {
			mainPanel.remove(flexTable);
		}
		flexTable = new FlexTable();
		flexTable.setBorderWidth(1);
	//	flexTable.setWidth("100%");

		// Add these lines
		flexTable.setText(0, 0, "Project Title");
		flexTable.setText(0, 1, "Type");
		flexTable.setText(0, 2, "Description");
		flexTable.setText(0, 3, "Tutors");
		flexTable.setText(0, 4, "Modify");
		flexTable.setText(0, 5, "Remove");
		
		projectService.retrieveProjects(new AsyncCallback<List<ProjectDTO>>() {
			
			@Override
			public void onSuccess(List<ProjectDTO> result) {
				for(int i = 0; i < result.size(); ++i){
					final ProjectDTO tmp = result.get(i);
					
					flexTable.setText(i+1, 0, tmp.getTitle());
					flexTable.setText(i+1, 1, tmp.getType().toString());
					flexTable.setText(i+1, 2, tmp.getDescription());
					
					String tutorNameList = "";
					java.util.Iterator<UserDTO> it = tmp.getTutors().iterator();
					while(it.hasNext()) {
						tutorNameList += it.next().getUserName();
				        if(it.hasNext()){
				        	tutorNameList += ", ";
				        }
				      }
					flexTable.setText(i + 1, 3, tutorNameList);

					// add button to modify this project from the list
				    Button modifyProject = new Button("Modify");
				    modifyProject.addClickHandler(new ClickHandler() {
				    public void onClick(ClickEvent event) {	
						projectService.retrieveProject(tmp.getProjectId(), new AsyncCallback<ProjectDTO>() {
							@Override
							public void onSuccess(ProjectDTO result) {
						    	new ModifyProject(result).center();
							}

							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Remote Procedure Call - Failure");
							}
						});
				    }
				    });
				    
					flexTable.setWidget(i+1, 4, modifyProject);
					
					 // add button to remove this project from the list
				    Button removeUser = new Button("Delete");
				    removeUser.addClickHandler(new ClickHandler() {
				    public void onClick(ClickEvent event) {	
				    	//Remove the User - inner AsyncCall
				    	projectService.deleteProject(tmp.getProjectId(), new AsyncCallback<Boolean>() {
							@Override
							public void onSuccess(Boolean result) {
								if(result == true){
									Window.confirm("'"+tmp.getTitle() + "' project deleted!");		
							        //Refresh the table
							        loadProjects();
								}else{
									Window.alert("Project deletion failed!");
								}
							}
							
							@Override
							public void onFailure(Throwable caught) {
								Window.alert("Remote Procedure Call - Failure");
							}
						});	       
				    }
				    });
				    flexTable.setWidget(i+1, 5, removeUser);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Remote Procedure Call - Failure");
			}
		});
		mainPanel.add(flexTable);
	}

}
